package com.wwz;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.*;

/**
 * @author:wwz
 * @Date: 2020-12-16 14:27
 */
//public class ScheduleExportTemplate {
//
//    /**
//     * @Description: 排期查询导出
//     * @create: 2020-12-24 11:12:39
//     * @author:wwz
//     */
//    public static void getScheduleExportExcel(List<ResourceScheduleVO> resourceScheduleVOList, HttpServletResponse response) throws Exception {
//
//        Workbook workbook = new XSSFWorkbook();
//        Sheet sheet = workbook.createSheet("排期查询");
//        CellStyle cellStyle = centerStyle(workbook);
//
//        Map<String, String> aliasNameMap = new TreeMap<>();
//        Set<Integer> dateSet = new HashSet<Integer>();
//        int i = 0;
//        for (ResourceScheduleVO re : resourceScheduleVOList) {
//            i++;
//            String aliasName = re.getAliasName();
//            aliasNameMap.put(String.valueOf(i), aliasName);
//            //获取日期
//            Set<Map.Entry<Integer, StockScheduleVO>> entries = re.getStockScheduleMap().entrySet();
//            for (Map.Entry<Integer, StockScheduleVO> entry : entries) {
//                dateSet.add(entry.getKey());
//            }
//        }
//
//        //创建表头
//        createHeader(sheet, dateSet.size());
//        //创建第二行
//        Row rowTwo = sheet.createRow(1);
//        rowTwo.createCell(0).setCellValue("资源位置");
//        rowTwo.getCell(0).setCellStyle(cellStyle);
//        //合并单元格
//        sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
//        List<Integer> dateList = new ArrayList<>(dateSet);
//        //创建第三行
//        Row rowThree = sheet.createRow(2);
//        for (int j = 0; j < dateSet.size(); j++) {
//            DateTime date = DateTime.parse(String.valueOf(dateList.get(j)), DateTimeFormat.forPattern("yyyyMMdd"));
//            rowTwo.createCell(j + 1).setCellValue(date.toString("yyyy/MM/dd"));
//            rowTwo.getCell(j + 1).setCellStyle(cellStyle);
//            //填充星期
//            rowThree.createCell(j + 1).setCellValue(date.toString("EE", Locale.CHINA));
//            rowThree.getCell(j + 1).setCellStyle(cellStyle);
//        }
//        Set<String> keySet = aliasNameMap.keySet();
//
//        //写入资源位信息
//        writeAliasName(keySet, sheet, aliasNameMap, cellStyle);
//        //异构数据
//        HashMap<String, ResourceScheduleVO> resourceScheduleVOMap = new HashMap<>();
//        for (ResourceScheduleVO re : resourceScheduleVOList) {
//            resourceScheduleVOMap.put(re.getAliasName(), re);
//        }
//        List<String> keySetList = new ArrayList<>(keySet);
//        //写入内容
//        writeContent(workbook, keySetList.size(), aliasNameMap, resourceScheduleVOMap, sheet, dateList);
//        //设置样式
//        setStyle(workbook, sheet, dateList.size(), resourceScheduleVOList.size());
//        LocalDate localDate = LocalDate.now();
//        String fileName = URLEncoder.encode("排期查询-" + localDate.toString().replace(SeparateConstant.MIDDLE_LINE, SeparateConstant.PERIOD)+".xlsx", "UTF-8");
//        response.setHeader("Content-disposition", "attachment;filename="+fileName+";"+"filename*=utf-8''"+fileName);
//        OutputStream outputStream = response.getOutputStream();
//        workbook.write(outputStream);
//        outputStream.flush();
//        workbook.close();
//
//    }
//
//    /**
//     * @Description: 创建表头
//     * @create: 2020-12-24 10:12:40
//     * @author:wwz
//     */
//    public static void createHeader(Sheet sheet, int width) {
//        //创建第一行
//        Row row0 = sheet.createRow(0);
//        row0.createCell(0).setCellValue("备注");
//        row0.createCell(1).setCellValue("1、深色填充表示当天所有库存已售。2、表格为空表示当天全部库存可售。3、表格内容为当天售卖或预占信息。");
//        //合并单元格
//        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, width));
//    }
//
//    /**
//     * @Description: 写入资源位信息
//     * @create: 2020-12-24 10:12:08
//     * @author:wwz
//     */
//    public static void writeAliasName(Set<String> keySet, Sheet sheet, Map<String, String> aliasNameMap, CellStyle cellStyle) {
//        int rowNum = 3;
//        for (String key : keySet) {
//            Row row = sheet.createRow(rowNum++);
//            String aliasName = aliasNameMap.get(key);
//            int cellNum = 0;
//            row.createCell(cellNum).setCellValue(aliasName);
//            row.getCell(cellNum).setCellStyle(cellStyle);
//            cellNum++;
//        }
//    }
//
//    /**
//     * @Description: 写入内容
//     * @create: 2020-12-24 10:12:56
//     * @author:wwz
//     */
//    public static void writeContent(Workbook workbook, int num, Map<String, String> aliasNameMap, HashMap<String, ResourceScheduleVO> resourceScheduleVOMap, Sheet sheet, List<Integer> dateList) {
//        for (int j = 0; j < num; j++) {
//            Collection<String> values = aliasNameMap.values();
//            ArrayList<String> strings = new ArrayList<>(values);
//            ResourceScheduleVO resourceScheduleVO = resourceScheduleVOMap.get(strings.get(j));
//            //初始加三
//            int n = j + 3;
//            Row row = null;
//            if (sheet.getRow(n) == null) {
//                row = sheet.createRow(n);
//            } else {
//                row = sheet.getRow(n);
//            }
//            for (int k = 0; k < dateList.size(); k++) {
//                //初始加1
//                int colNum = k + 1;
//                //找对应日期下面的内容
//                StockScheduleVO stockScheduleVO = resourceScheduleVO.getStockScheduleMap().get(dateList.get(k));
//                if (stockScheduleVO == null)
//                    continue;
//                String title = "";
//                List<UnionScheduleDetailVO> scheduleDetailList = stockScheduleVO.getScheduleDetailList();
//                for (UnionScheduleDetailVO scheduleDetail : scheduleDetailList) {
//                    //拼接标题
//                    title += scheduleDetail.getTitle() + "\r\n";
//                }
//                CellStyle style = workbook.createCellStyle();
//                style.setWrapText(true);
//                style.setBorderTop(BorderStyle.THIN);
//                style.setBorderBottom(BorderStyle.THIN);
//                style.setBorderLeft(BorderStyle.THIN);
//                style.setBorderRight(BorderStyle.THIN);
//                row.createCell(colNum).setCellStyle(style);
//                row.getCell(colNum).setCellValue(new XSSFRichTextString(title));
//
//                List<ResourceStockVO> originResourceStockList = resourceScheduleVO.getOriginResourceStockList();
//                for (ResourceStockVO resourceStockVO : originResourceStockList) {
//                    if (resourceStockVO.getStatus() == 0) {
//                        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//                        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//                        row.getCell(colNum).setCellStyle(style);
//                        break;
//                    }
//                }
//            }
//        }
//    }
//
//    /**
//     * @Description: 设置样式
//     * @create: 2020-12-24 10:12:52
//     * @author:wwz
//     */
//    private static void setStyle(Workbook workbook, Sheet sheet, int columnSize, int rowSize) {
//        CellStyle style = workbook.createCellStyle();
//        style.setBorderTop(BorderStyle.THIN);
//        style.setBorderBottom(BorderStyle.THIN);
//        style.setBorderLeft(BorderStyle.THIN);
//        style.setBorderRight(BorderStyle.THIN);
//        for (int columnNum = 0; columnNum <= columnSize; columnNum++) {
//            if (columnNum == 0) {
//                sheet.setColumnWidth(columnNum, 25 * 256);
//            } else {
//                sheet.setColumnWidth(columnNum, 35 * 256);
//            }
//        }
//        //添加黑边框
//        for (int i = 1; i < rowSize + 3; i++) {
//            for (int j = 0; j <= columnSize; j++) {
//                if (i == 1 & j == 0) {
//                    break;
//                }
//                if (sheet.getRow(i).getCell(j) == null) {
//                    sheet.getRow(i).createCell(j).setCellStyle(style);
//                }
//            }
//        }
//    }
//
//    /**
//     * @Description: 居中样式
//     * @create: 2020-12-24 11:12:06
//     * @author:wwz
//     */
//    public static CellStyle centerStyle(Workbook workbook) {
//        CellStyle cellStyle = workbook.createCellStyle();
//        //水平居中
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);
//        //垂直居中
//        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        cellStyle.setBorderTop(BorderStyle.THIN);
//        cellStyle.setBorderBottom(BorderStyle.THIN);
//        cellStyle.setBorderLeft(BorderStyle.THIN);
//        cellStyle.setBorderRight(BorderStyle.THIN);
//        return cellStyle;
//    }
//}
